﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.OA.Email
{
    public partial class email_wdyj_index : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {

            Response.ContentEncoding = System.Text.Encoding.UTF8;
            if (!IsPostBack)
            {
                InitPage();
            }

            BindGridView();
        }        

        private void InitPage() 
        {
            string sPaceLast = string.Empty;
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            hidvar.Value = loginUser.UserId;
            string userId = hidvar.Value;
            EmailBLL emailBLL = new EmailBLL();
            emailBLL.EmailShowTable(userId, tdEmailShow, "1", ref sPaceLast);
            hidspacelast.Value = sPaceLast;
            emailBLL.Release();
            emailBLL = null;
        }

        private void BindGridView()
        {
            grid.DataSource = GetDateBind();
            grid.DataBind();
        }

        private DataSet GetDateBind()
        {
            string UserId = hidvar.Value;
            EmailBLL bll = new EmailBLL();
            DataSet dSet = bll.EmailFolderBind(UserId);
            bll.Release();
            bll = null;
            return dSet;
        }


        /// <summary>
        /// 删除ajax方法 删除邮件夹及其下边所有邮件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteFolders(string serExpr, string operType)
        {
            bool bFlag = false;
            MsEmailRecord msRecord = (MsEmailRecord)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailRecord));
            EmailBLL bll = new EmailBLL();
            bFlag = bll.DelEmailFolder(msRecord);
            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { operType = operType, resultSign = sResult };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msRecord = null;
            return resultSerExpr;
        }


        /// <summary>
        /// 删除ajax方法 邮件夹下边所有邮件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteEmail(string serExpr, string operType)
        {
            bool bFlag = false;
            MsEmailRecord msRecord = (MsEmailRecord)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailRecord));
            EmailBLL bll = new EmailBLL();
            bFlag = bll.DelFolderClerEmali(msRecord);
            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { operType = operType, resultSign = sResult };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msRecord = null;
            return resultSerExpr;
 
        }

        protected void grid_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            #region 更新列值
            string fileSum = grid.GetRowValues(e.VisibleIndex, "FILESUM").ToString().Trim();
            string fileTitle = grid.GetRowValues(e.VisibleIndex, "TITLE").ToString().Trim();
            string fileType = grid.GetRowValues(e.VisibleIndex, "TYPE").ToString().Trim();
            string fileID = grid.GetRowValues(e.VisibleIndex, "FolderID").ToString().Trim();
            string fileYesNo = grid.GetRowValues(e.VisibleIndex, "STATE").ToString().Trim();
            string fileXYJ = grid.GetRowValues(e.VisibleIndex, "XYJ").ToString().Trim();  //新邮件数量
            string fileCount = grid.GetRowValues(e.VisibleIndex,"FILECOUNT").ToString().Trim();

            string sparam = "YJJXH=" + fileID + "&TITLE=" + fileTitle + "&YJBZ=" + fileType;
            string encodeparam = Base64Lib.Encode(sparam);
            if (fileXYJ != "0")
            {

                e.Row.Cells[1].Text = "<a href=\"email_manages_frame.aspx?" + encodeparam + "\"><b>" + fileTitle + "</b></a>";
                e.Row.Cells[2].Text = "<b> <font color='red'>" + fileXYJ + "</font></b>";
            }
            else {
                e.Row.Cells[1].Text = "<a href=\"email_manages_frame.aspx?" + encodeparam + "\">" + fileTitle + "</a>";
                    e.Row.Cells[2].Text = fileXYJ;

            }

            if (fileCount != "0")
            {
                e.Row.Cells[3].Text = "<b>"+ fileCount +"</b>";
            }

            e.Row.Cells[4].Text = fileSum + "K";

            if (fileYesNo == "0")
            {
                e.Row.Cells[5].Text = "无效";
            }
            else if (fileYesNo == "1")
            {
                e.Row.Cells[5].Text = "有效";
            }
            #endregion

            #region 操作列按钮
            LinkButton lnbModify = (LinkButton)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbModify");
            LinkButton lnbDel = (LinkButton)this.grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbDel");
            LinkButton lnbDelemail = (LinkButton)this.grid.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbDelemail");

           

            if (fileID == "0")
            {
                lnbModify.Enabled = false;
                lnbDel.Enabled = false;
            }
            else {
                lnbDel.Attributes["onclick"] = "javascript:doDeleteFolder('" + fileID + "','" + fileType + "');return false;"; 
                lnbModify.Attributes["onclick"] = "javascript:return doEditFolder('" + encodeparam + "');";
            }
            lnbDelemail.Attributes["onclick"] = "javascript:doDeleteEmail('"+fileID+"','" + fileType + "');return false;";
            
           

            #endregion
        }

    }
}
